package com.custom.readerviewpager.View;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.viewpager.widget.ViewPager;

/**
 * @description：水平切换一屏多个视图滚动
 * @Author MRyan
 * @Date 2020/3/20 14:53
 * @Version 1.0
 */
public class CustomReaderViewPager2 extends ViewPager {
    public CustomReaderViewPager2(Context context) {
        this(context, null);
    }

    public CustomReaderViewPager2(Context context, AttributeSet attrs) {
        super(context, attrs);
        setReadEffect();
    }


    @SuppressLint("ClickableViewAccessibility")
    private void setReadEffect() {
        setPageTransformer(true, new PageTransformer() {//reverseDrawingOrder 返回True前面的视图可以遮住后面的视图
            private float MIN_SCALE = 0.8f;//初始
            private float MIN_ALPHA = 0.3f;

            @Override
            public void transformPage(View page, float position) {
                float scaleFactor = Math.max(MIN_ALPHA, 1 - Math.abs(position));//[1->0.3] or [0.3->1]
                if (position < -1 || position > 1) {//前一个或者后一个 大小缩小0.5并且透明度减少0.5
                    page.setScaleX(MIN_SCALE);
                    page.setScaleY(MIN_SCALE);
                    page.setAlpha(scaleFactor);
                } else if (position <= 1) {
                    if (position < 0) {//右滑时前一个View position=[-1->0) ||左滑时当前View position=（0->1]
                        float scaleX = 1 + 0.2f * position;// 右滑时前一个View scaleX=0.8->1 || 左滑当前View scaleX=1->0.8
                        page.setScaleX(scaleX);
                        page.setScaleY(scaleX);
                    } else { //右滑当前View position=(0->1]
                        float scaleX = 1 - 0.2f * position;
                        page.setScaleX(scaleX);
                        page.setScaleY(scaleX);
                    }
                    page.setAlpha(scaleFactor);
                }
            }
        });
    }
}